package com.kuzan.autocloud.client.sys.config.controller;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.kuzan.autocloud.client.sys.config.service.CMenuService;
import com.kuzan.autocloud.common.Result;
import com.kuzan.autocloud.common.repository.menu.MenuGroupRepository;
import com.kuzan.autocloud.common.repository.menu.MenuItemRepository;
import com.kuzan.autocloud.common.repository.menu.MenuRootRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>Description: 菜单配置api</p>
 *
 * @author kuzank 2019/12/2
 */
@RestController
@RequestMapping({"/c/menu"})
public class CMenuController {

    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();

    @Autowired
    CMenuService cMenuService;
    @Autowired
    MenuRootRepository rootRepository;
    @Autowired
    MenuGroupRepository groupRepository;
    @Autowired
    MenuItemRepository itemRepository;


    @PostMapping("getAllRoot")
    public Result getAllRoot() {
        return Result.ofsuccess(rootRepository.findAllByOrderBySortAsc());
    }

    @PostMapping("rootOnDrop")
    public Result rootOnDrop(@RequestBody List<String> ids) {
        return Result.ofsuccess(cMenuService.rootOnDrop(ids));
    }

    @PostMapping("rootOnCreate")
    public Result rootOnCreate(@RequestBody Map map) {
        return Result.ofsuccess(cMenuService.rootOnCreate(map));
    }

    @PostMapping("rootOnDelete/{id}")
    public Result rootOnDelete(@PathVariable("id") String id) {
        return Result.ofsuccess(cMenuService.rootOnDelete(id));
    }

    @PostMapping("rootOnEdit")
    public Result rootOnEdit(@RequestBody Map map) {
        return Result.ofsuccess(cMenuService.rootOnEdit(map));
    }

    @PostMapping("getGroupsByRootId/{id}")
    public Result getGroupsByRootId(@PathVariable("id") String id) {
        return Result.ofsuccess(groupRepository.findAllByBelongOrderBySortAsc(id));
    }

    @PostMapping("getGroupInfoById/{id}")
    public Result getGroupInfoById(@PathVariable("id") String id) {
        return Result.ofsuccess(cMenuService.getGroupInfoById(id));
    }

    @PostMapping("groupOnDrop")
    public Result groupOnDrop(@RequestBody List<String> ids) {
        return Result.ofsuccess(cMenuService.groupOnDrop(ids));
    }
}
